//
//  WDCustomMenuItem.m
//  Zhihuijinfu
//
//  Created by 王笛 on 2017/3/2.
//  Copyright © 2017年 王笛. All rights reserved.
//

#import "WDCustomMenuItem.h"

#define viewY 44
#define btnH 40.5
#define fontSize 15
#define Duration 1
#define marginH 5

@interface WDCustomMenuItem ()
/** 显示的View */
@property (nonatomic, strong) UIView *contentView;
/** tableView */
@property (nonatomic, strong) UITableView *tableView;

@end

@implementation WDCustomMenuItem

static NSArray * allbus = nil;
static NSInteger seleNum = 0;

- (WDCustomMenuItem *)initWithDataArr:(NSArray *)dataArr seleIndex:(NSInteger)seleIndex
{
    allbus = dataArr;
    seleNum = seleIndex;
    
    WDCustomMenuItem * menuItem = [[WDCustomMenuItem alloc]initWithFrame:CGRectMake(0, viewY, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - viewY)];
    [menuItem set];
    return menuItem;
}

-(void)set
{
    [UIView animateWithDuration:Duration animations:^{
        _contentView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, btnH*(allbus.count) + marginH);
    }];
    
}

- (void)hide
{
    [self removeFromSuperview];
}

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self=[super initWithFrame:frame])
    {
        UIView *back = [[UIView alloc]initWithFrame:CGRectMake(0, viewY, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - viewY)];
        back.backgroundColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.3];
        [self addSubview:back];
        
        _contentView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WDScreenW, 0)];
        _contentView.backgroundColor = [UIColor whiteColor];
        [self addSubview:_contentView];
        
        for (int i = 0; i<allbus.count; i++)
        {
            UIButton * bu = [UIButton buttonWithType:UIButtonTypeCustom];
            
            if (i == seleNum) {
                bu.selected = YES;
            } else {
                bu.selected = NO;
            }
            
            bu.tag = i;
            bu.backgroundColor = [UIColor whiteColor];
            bu.frame = CGRectMake(0, btnH * i + marginH, [UIScreen mainScreen].bounds.size.width, btnH);
            [_contentView addSubview:bu];
            bu.titleLabel.font = [UIFont systemFontOfSize:fontSize];
            [bu setTitle:allbus[i] forState:UIControlStateNormal];
            [bu setTitleColor:ColorForRGB(0x6b6b6b) forState:UIControlStateNormal];
            [bu setTitleColor:WDNavSeleColor forState:UIControlStateSelected];
            [bu setBackgroundImage:[UIImage imageWithColor:[UIColor colorWithWhite:0.8 alpha:0.4]] forState:UIControlStateHighlighted];
            [bu addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
        }
    }
    return self;
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [_delegate touchMove];
    [self removeFromSuperview];
}
-(void)clickButton:(UIButton*)button
{
    [_delegate  customMenuItem:self clickIndex:button.tag];
    
    [self removeFromSuperview];
}


@end
